package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.api.domain.SkuProperty;
import cn.wolfcode.shop.api.service.ISkuPropertyService;
import cn.wolfcode.shop.api.vo.JsonResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SkuPropertyController {

    @Reference
    private ISkuPropertyService skuPropertyService;

    /**
     * sku页面跳转
     *
     * @return
     */
    @RequestMapping("skuProperty")
    public String skuPropertyPage() {
        return "sku/sku_property";
    }


    /**
     * 点击子菜单显示的sku属性列表
     *
     * @param catalogId
     * @param model
     * @return
     */
    @RequestMapping("skuProperty/get/{catalogId}")
    public String getSkuProperty(@PathVariable("catalogId") Long catalogId, Model model) {
        List<SkuProperty> list = skuPropertyService.getSkuProperty(catalogId);
        model.addAttribute("list", list);
        return "sku/property_list";
    }

    /**
     * 打开添加sku属性列表
     */
    @RequestMapping(value = "skuProperty/save", method = RequestMethod.GET)
    public String skuPropertyAdd(SkuProperty skuProperty, Model model) {
        if (skuProperty.getId() != -1) {
            model.addAttribute("skuProperty", skuPropertyService.get(skuProperty.getId()));
        }
        return "sku/property_save";
    }

    /**
     * 添加sku属性
     *
     * @param skuProperty
     * @return
     */
    @RequestMapping(value = "skuProperty/save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultVo addSkuProperty(SkuProperty skuProperty) {
        JsonResultVo jsonResultVo = new JsonResultVo();
        try {
            if (skuProperty.getId() == null || skuProperty.getId() == -1) {
                skuProperty.setSequence(0);
                skuPropertyService.save(skuProperty);
            } else {
                SkuProperty newSkuProperty = skuPropertyService.get(skuProperty.getId());
                newSkuProperty.setName(skuProperty.getName());
                newSkuProperty.setType(skuProperty.getType());
                skuPropertyService.update(newSkuProperty);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultVo.setErrorMsg(e.getMessage());
        }
        return jsonResultVo;
    }

    /**
     * 删除属性
     */
    @RequestMapping(value = "skuProperty/delete")
    public String deleteSkuProperty(SkuProperty skuProperty) {
        JsonResultVo jsonResultVo = new JsonResultVo();
        try {
            if (skuProperty.getId() != null) {
                skuPropertyService.delete(skuProperty.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultVo.setErrorMsg("删除失败");
        }
        StringBuilder sb = new StringBuilder(50);
        sb.append("redirect:/skuProperty/get/").append(skuProperty.getCatalogId());
        return sb.toString();
    }

}
